<?php
function ListAllTour()
{
    global $dbsql,$language,$locationid,$html,$cid,$typeid;
    
    
    $locationid = intval($locationid);
    $sql_where  = " from tblsettour where cLanguage='$language' and iStatus=1 ";
    
    if(intval($typeid)>0)
    {
        $sql_where.= ' and iParentId = '.$typeid;
    }
   
    $sql_select = "select count(iId) as count ".$sql_where;
    $result     = $dbsql->query_fetch($sql_select);
    $countall   = $result['count'];
    
    list($lat,$lall)    = Split_Page($countall);
    
    $sql_select = "select iId,iParentId,cName,cUrlString,cDays,cSummary,cPriceFrom,cImage,iPromotion ".$sql_where;
    $sql_select.= " order by iOrder asc limit $lat,$lall";
    $rs         = $dbsql->query($sql_select);
    $rows       = $dbsql->num_rows($rs);
    $return     = '<div class="ajaxpage">';
    $i          = 0;
    
    while($result   = $dbsql->fetch_array($rs))
    {
        $url    = GetLinkTourById($result['iId'],$result['cUrlString']);    
        $class  = ($i%2==0)? 'row1': 'row2';     
        $i ++;
        
        $msg    =  mb_substr(strip_tags($result['cSummary']),0,420);    
        $return .='<div class="tour '.$class.'">';
        $return .='<a class="thumb" href="'.$url.'"><img src="'.GetIntroImg($result['cImage'],true).'" width=130></a>';
        $return .='<a class="title" href="'.$url.'">'.$result['cName'].'</a>';
        $return .='<div class="des">'.$msg.'</div>';  
        $return .= '<a class="readmore" href="'.$url.'">'.__('detail').'</a>';              
        $return .='<div class="clear">&nbsp;</div></div>';        
    }
    if($rows==0)
    {
        $return .= '<div class="notfound">'.getLang('Not found result!').'</div>';
    }
    $return     .= "<div class='clear'>&nbsp;</div></div>";
    return $return;
}

function GetLinkTourById($idtour,$urltour='')
{
    global $dbsql,$language,$html;
    
    $idtour     = intval($idtour);
    $sql_select = "select iParentId,cUrlString from tblsettour where iId = $idtour limit 1";
    $tour       = $dbsql->query_fetch($sql_select);
    $link_tour  = $tour['cUrlString'];
    $tour['iParentId']  = intval($tour['iParentId']);
    $sql_select = "select cUrlString from tbltourtype where iId = '".$tour['iParentId']."'";
    $tourtype   = $dbsql->query_fetch($sql_select);

    $link       = getUrlCatByType('tour').'/'. $tourtype['cUrlString'] .'/'.$link_tour.'.htm';
    
    return $link;
}

function ListIdTourByListLocationId($idloc,$returnarray=false)
{
    global $dbsql,$language;
    if(is_array($idloc))
    {
        $idloc  = implode(',',$idloc);
    }
    $list       = array();
    $sql_select = "select iTourId from tblsettour_location where iLocationId in (".$idloc.")";
    $rs         = $dbsql->query($sql_select);
    while($result   = $dbsql->fetch_array($rs))
    {
        $list[]     = $result['iTourId'];
    }
    if($returnarray==false)
    {
        $list   = implode(',',$list);
        if(empty($list))
        {
            $list   = '-1';
        }
    }
    return $list;
}

function ListPhotosTour($id)
{
    global $dbsql,$language;
    $id         = intval($id);
    $sql_select = "select cImage,cName from tblgallery where cType='tour' and iParentId=$id";
    return $dbsql->load($sql_select);
    return $return;
}

function ListVideoTour($id)
{
    global $language,$dbsql;

	$sql_select = "SELECT * FROM tblsettour_video WHERE iParentId = '$id' order by iOrder ";
    return $dbsql->load($sql_select);
}

function ListOtherTour($idtype,$idloc,$id)
{
    global $dbsql,$language;
    
    $id         = intval($id);
    $idtype     = intval($idtype);
    $idloc      = intval($idloc);
    $sql_select = "select iId,cUrlString,cName,cImage,cDays,cPriceFrom,cHighLights
                 from tblsettour  where(iParentId=$idtype) and iId!=$id and iStatus=1 order by iOrder limit 6";
    return $dbsql->load($sql_select);
}

function BuildTourtype()
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cName from tbltourtype where cLanguage='$language'";
    $rs         = $dbsql->query($sql_select);
    $endline    = '';
    $return     = '';
    while($result   = $dbsql->fetch_array($rs))
    {
        $return .= 'totcb.add({id: '.$result['iId'].',name: "'.$result['cName'].'",g: 0});'.$endline; 
    }
    return $return;
}


function ListTourByLocation($idloc,$limit=10)
{
    global $dbsql,$language;
    $idloc      = intval($idloc);
    $sql_select = "select st.iId,st.cUrlString,st.cName,st.cImage,st.cMetaDescription 
                    from tblsettour as st JOIN tblsettour_location as sl ON
                    st.iId = sl.iTourId and sl.iLocationId=$idloc
                    and st.cLanguage= '$language' order by st.iOrder limit $limit";
    $rs         = $dbsql->query($sql_select);
    $linktourroot= LinkTourRoot().LinkLocationById($idloc);
    $tours       = array();
    while($result   = $dbsql->fetch_array($rs))
    {
        $result['cUrlString']= $linktourroot.'/'.$result['cUrlString'].'.htm';
        $tours[]             = $result;
    }   
    return $tours;
    
}

function ListTourByListLocationId($locs,$limit=10)
{
    global $dbsql,$language;
    
    $listlocs   = ListIdLocationChild($locs);
    $tourids    = ListIdTourByListLocationId($listlocs);
    
    $sql_select = "select iId,cUrlString,cName,cImage,cMetaDescription 
                    from tblsettour where iId in ($tourids)
                    and cLanguage= '$language' order by iOrder limit $limit";
    $rs         = $dbsql->query($sql_select);
    $linktourroot= LinkTourRoot().LinkLocationById($idloc);
    $tours       = array();
    while($result   = $dbsql->fetch_array($rs))
    {
        $result['cUrlString']= $linktourroot.'/'.$result['cUrlString'].'.htm';
        $tours[]             = $result;
    }   
    return $tours;
}

function LinkTourRoot()
{
    global $dbsql,$language;
    $sql_select = "select cUrlString from tblcategory where cType='tour' and cLanguage='$language' limit 1";
    $result     = $dbsql->query_fetch($sql_select);
    $Link       = '/'.$result['cUrlString'];
    return $Link;
}

function getLinkSendInquiry()
{
    global $dbsql,$language,$html;
    
    $sql_select = "select cUrlString from tblcategory where cType='inquiry' and cLanguage='$language'";
    $result     = $dbsql->query_fetch($sql_select);
    return '/'.$html->urlRoot.'/'.$result['cUrlString'].'/';
}

function InsertBooking()
{
    global $dbsql,$language;
    require_once "includes/php-captcha.inc.php";
    
    if (PhpCaptcha::Validate($_POST['SpamCode'])==false)
    {
         AjaxTransfer('spam',getLang('Please enter the correct security code!'));
    }
    
	$ip              = $_SERVER['REMOTE_ADDR'];
	$datebooking     = date("Y/m/d");
	
	//Personal Details
	$FirstName 		= gpc_getStringPost('FirstName');
	$LastName		= gpc_getStringPost('LastName');
	$FullName		=$FirstName." ".$LastName;
	
	$Email 			= gpc_getStringPost('Email'); 
	$Phone 			= gpc_getStringPost('Phone');
	$Country 		= gpc_getStringPost('Country');
	
	//tour:
    $tourname       = gpc_getStringPost('tourname');
 	$arrivalDate 	= gpc_getStringPost('arrivalDate');
 	$departureDate 	= gpc_getStringPost('departureDate');
	
	$Adult 			= gpc_getStringPost('Adult');
 	$Childrent 		= gpc_getStringPost('Childrent');
 	$Infants 		= gpc_getStringPost('Infants');
	
 	$selectr 		= gpc_getStringPost('selectr');
 	$TellUs1 		= gpc_getStringPost('TellUs1');
 	$TellUs2 		= gpc_getStringPost('TellUs2');
 	$Destinations 	= gpc_getStringPost('Destinations');
	$Special 		= gpc_getStringPost('Special');
    $newsletter     = gpc_getBoolPost('newsletter');
    
    ImportClass('country');
    $CountryObj     = new Country();
    $Country        = $CountryObj->getNameByCode($Country);
    
    $emailconfig       = @getConfigEmail('TourBookingEmail');
    
    if($emailconfig==false)
    {
        //AjaxTransfer('config','Email not config');
    }

 	//insert to booking -------------------------------
	$sql_insert = "INSERT INTO tblbooking
	(cFName,cLName,cEmail,cPhone,cAddress,cCountry,cArrivalDate,cDepartureDate,
	cAdult,cChildrent,cInfants,cTourName,
	cTellUs1,cTellUs2,cDestinations,cSpecial,iStatus,dDay,cLanguage) 
	VALUES 
	('$FirstName','$LastName','$Email','$Phone','$Address','$Country','$arrivalDate','$departureDate',
	'$Adult','$Childrent','$Infants','$tourname',
	'$TellUs1','$TellUs2','$Destinations','$Special','0','$datebooking','$language')";
	
	$dbsql->query($sql_insert);	

	//--------------------send to email:------------------------
 	$message_mail="
	<b>I. ".getLang('PERSONAL DETAILS')."</b>
	<div style='padding-left:40'>
		".getLang('Full Name').": ".$FullName ."<br>
		".getLang('Email').": ".$Email."<br>
		".getLang('Phone').": ".$Phone."<br>
		".getLang('Country').": ".$Country."<br>			
		</div><br><br>	
	
	<b>II.  ".getLang('TOUR INFO')."</b>
	<div style='padding-left:40'>
	".getLang('Tour Name').": ".$tourname."<br />    
	".getLang('Arrival Date').": ".$arrivalDate."<br />
	".getLang('Departure Date').": ".$departureDate."<br />
	".getLang('Adults').": ". $Adult."<br />
	".getLang('Childs').": ".$Childrent."<br />
	".getLang('Infants').": ".$Infants."<br />";
    if(!empty($TellUs1))
    {
        $message_mail .=" <div style='padding-left:15px'>".$TellUs1."</div><br />";
    }
    if(!empty($Destinations))
    {
        $message_mail .=getLang('Destinations').": ".$Destinations."<br />";
    }
	$message_mail .=getLang('Special interests').": <div style='padding-left:15px'>".$Special."</div><br>
	<br><br>
 	IP:".$ip;
	
    
	$subject                = $emailconfig->title;
    $subject                = str_ireplace('{TOURNAME}',$tourname,$subject);
    $subject                = str_ireplace('{NAME}',$FullName,$subject);
    $subject                = str_ireplace('{email}',$Email,$subject);
    $emailconfig->content   = str_ireplace('{NAME}',$FullName,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{TOURNAME}',$tourname,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{BOOKINFO}',$message_mail,$emailconfig->content);
    $emailconfig->content   = str_ireplace('{message}',$message_mail,$emailconfig->content);
    
    
 	//send to admin
 	$rs   = send_email($emailconfig->email,"",$subject,$message_mail);
 	if($rs==false)
    {
       // AjaxTransfer('ok','Send Email False please contact to admin');
    }
 	//send to cus---------------------	   
     
	$rs   = send_email($Email,"",$subject,$emailconfig->content);     	
	if($rs==false)
    {
        //AjaxTransfer('ok','Send Email False please contact to admin');
    }
	//save email newletter--------------------------------------------
	if($newsletter)
 	{
        @ImportClass('newsletter');
 		@regnewsletter($Email,$FullName);
 	}
    
    
    $datasuccess    = getConfig('BookTourSuccess');
    $datasuccess    = str_replace('{NAME}',$FullName,$datasuccess);
    
	AjaxTransfer('ok',$datasuccess);
}
?>